package com.zhao.rabbitmq.five;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;
import com.zhao.rabbitmq.utils.RabbitMQUtils;

/**
 * @author xiaoZhao
 * @date 2022/12/12
 * @describe 负责接收消息
 */
public class ReceiveLogs01 {

    // 交换机名称
    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        //   声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");

        /**
        * 声明的队列 临时队列
         *  生成一个临时队列 队列名称是随机的
         *  当消费者断开与队列的连接 队列会自动删除删除
        **/
        String queueName = channel.queueDeclare().getQueue();

        // 绑定交换机与队列
        channel.queueBind(queueName,EXCHANGE_NAME,"");
        System.out.println("等待接收消息。。。。");

        // 接收消息
        DeliverCallback deliverCallback = (consumerTag,  message) ->{
            System.out.println("ReceiveLogs01接收到的消息："+new String(message.getBody(),"UTF-8"));
        };
        channel.basicConsume(queueName,true,deliverCallback,consumerTag -> {});
    }
}
